<template>
  <div class="auto">
    <validator name='v'>
      <p class="bg-info text-center" style="padding: 8px;">{{data.type}}撤销</p>
      <div class="row" style="margin-top:10px;">
        <div class="form-group">
          <label for="f_cancel_reason" class=" col-sm-2 control-label">撤销原因:</label>
          <div class="col-sm-8">
            <textarea class="form-control" v-model="model.f_cancel_reason" rows="3" placeholder="请填写原因"></textarea>
          </div>
        </div>
      </div>
      <div style="text-align:right;height:auto;margin-top:30px;margin-right:50px;">
        <button class="btn btn-success" @click="confirm()" >确&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;认</button>
        <button class="btn btn-default" @click="clean()" style="width:80px;margin-left:20px;">取&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;消</button>
      </div>
    </validator>
  </div>
</template>

<script>
import * as Util from '../../../Util'
export default {
  props: ['data'],
  title: '操作撤销',
  data () {
    return {
      model: {
        f_cancel_reason: '',
        f_cancel_name: Util.f.name,
        record_id: ''
      }
    }
  },
  methods: {
    confirm () {
      // 整理数据
      let name = this.sortdata()
      // 撤销操作请求
      this.post(name)
    },
    clean () {
      this.$dispatch('clean', this.model)
    },
    // 判断type类型  整理数据
    sortdata () {
      this.$info(`接受到操作记录数据：${JSON.stringify(this.data)}`)
      this.model.record_id = this.data.id
      let type = this.data.type
      let name
      // 根据type数据 选择调用的Logic
      if (type === '停用') {
        name = 'disableCancel'
      } else if (type === '启用') {
        name = 'enableCancel'
      } else if (type === '过户') {
        name = 'transferCancel'
      } else if (type === '换表') {
        name = 'changemeterCancel'
      } else if (type === '机表收费') {
        name = 'machineChargeCancel'
      } else if (type === '卡表收费') {
        name = 'cardChargeCancel'
      } else if (type === '发卡售气') {
        name = 'cardChargeCancel'
      } else if (type === '卡表超用') {
        name = 'cardOverCancel'
      } else if (type === '超用收费') {
        name = 'overuseCancel'
      } else if (type === '抄表员变更') {
        name = 'rollbackBatch'
      } else if (type === '气价变更') {
        name = 'rollbackBatch'
      } else if (type === '其他收费') {
        name = 'otherFeeRollback'
      } else if (type === '用户退费') {
        name = 'refundRollback'
      } else if (type === '补卡') {
        name = 'replaceCardCancel'
      } else {
        return
      }
      return name
    },
    // 发送请求  及 请求后处理页面
    post (name) {
      let url = 'rs/logic/' + name
      this.$info(`请求地址:${url}`)
      this.$info(`发送的数据, model: ${this.model}, recordData: ${this.data}`)
      this.$CancelService.cancel(url, this.model, this.data)
      .then((res) => {
        this.$info('撤销成功')
        this.$dispatch('success', this.data.type, this.model, res)
      })
      .catch((error) => {
        this.$info(`撤销失败 ${error}`)
        // this.$dispatch('error', this.data.type, this.model, error)
      })
    }
  }
}
</script>
